﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public static class StringExtension
    {
        public static string Enumerate(this IEnumerable<string> ar)
        {
        	return string.Join(", ", (ar ?? new string[0]).ToArray());
        }

        public static string goFormat(this string strk, params object[] args)
        {
            return string.Format(strk, args);
        }
        public static bool IsEmpty(this string strk)
        {
            return string.IsNullOrEmpty(strk);
        }
        public static string GetOrDefault(this string strk, string defString)
        {
            return (strk.IsEmpty()) ? defString : strk;
        }
        public static string Repeat(this string strk, int count)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++)            
                sb.Append(strk);            
            return sb.ToString();
        }
        public static int Count(this string strk, char inn)
        {
            int res = 0;
            for (int i = 0; i < strk.Length; i++)
                if (strk[i] == inn) res++;
            return res;
        }

        /// <summary>
        /// Если строка == null, то длина возвращается = -1
        /// </summary>
        /// <param name="strk"></param>
        /// <returns></returns>
        public static int GetLength(this string strk)
        {
            if (strk == null)
                return -1;
            else
                return strk.Length;
        }

        public static string GetSubstring(this string strk, int len)
        {
        	return strk != null && strk.Length > len 
				? strk.Substring(0, len) 
				: strk;
        }
    }
}
